package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.User;
import ejb.Facade;

/**
 * Servlet implementation class SupprimerEnvieServlet
 */
@WebServlet("/SupprimerEnvieServlet")
public class SupprimerEnvieServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SupprimerEnvieServlet() {
		super();
		// TODO Auto-generated constructor stub
	}
	/**
	 * Constructeur sans parametre
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public SupprimerEnvieServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user = (User) request.getSession().getAttribute("user");
		Facade fac = (Facade) request.getSession().getAttribute("Facade");
		//Recuperation de idWish
		try {
			int idWish = Integer.parseInt( request.getParameter("idWish") );

			//On supprime une envie
			fac.removeWish(user, idWish);

		}catch(Exception e) {
			System.err.println("Ce wish n'existe pas");
		}
		//Redirection
		new IndexServlet(request, response);
	}

}
